#include <jni.h>
#include <string>
#include "huffman.h"
#include <android/log.h>

extern "C"
JNIEXPORT jstring
JNICALL
Java_com_ndk_day49_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    //We build the tree depending on the string
    // 1.生成一棵最优的前缀码霍夫曼树
    htTree *codeTree = buildTree("aaaabbcccccccd");
    //We build the table depending on the Huffman tree
    // 2.根据霍夫曼树生成映射表
    hlTable *codeTable = buildTable(codeTree);

    // 3.编码 We encode using the Huffman table
    // std::string *res = new std::string();
    encode(codeTable,"aaaabbcccccccd");

    // encode(codeTable,"aaaabbcccccccd", res);
    // 打印输出 res->c_str() // 编码结果010101010010011111111000
    // __android_log_print(ANDROID_LOG_ERROR, TAG, "code = %s", res->c_str());
    delete(res);
    //We decode using the Huffman tree
    //We can decode string that only use symbols from the initial string
    // 4.解码
    decode(codeTree,"0011111000111");
    //Output : 0011 1110 1011 0001 0010 1010 1100 1111 1000 1001

    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
